package priv.pattern.prototypepattern;

import priv.pattern.prototypepattern.abs.Shape;
import priv.pattern.prototypepattern.abs.impl.Circle;
import priv.pattern.prototypepattern.abs.impl.Rectangle;
import priv.pattern.prototypepattern.abs.impl.Square;

import java.util.Hashtable;

/*
 * 作者：m鹏h
 * 时间；2024/7/4 21:55
 * 描述：用于存放一个复杂的对象的HashTable
 **/
public class ShapeCache {
    //用于存放复杂对象的HashTable
    private static Hashtable<String, Shape> shapeHashtable = new Hashtable<String, Shape>();

    //从HashTable中根据该对象的唯一属性 克隆出一个对象
    public static Shape getShape(String id) {
        Shape cachedShape = shapeHashtable.get(id);
        return (Shape) cachedShape.clone();
    }


    //把复杂的对象初始化到HashTable中
    public static void loadCache() {
        Circle circle = new Circle();
        circle.setId("1");
        shapeHashtable.put(circle.id, circle);

        Square square = new Square();
        square.setId("2");
        shapeHashtable.put(square.id, square);

        Rectangle rectangle = new Rectangle();
        rectangle.setId("3");
        shapeHashtable.put(rectangle.id, rectangle);
    }
}
